$(document).ready(function(){

	$("#member_id").select2({
		placeholder: "Select Client"
	});

	
/** Text Editor **/
//$('#message').wysihtml5();	
$('#nt_date1').datepicker({
            format: "dd/mm/yyyy",
           // startDate: '+1d',
            autoclose: true,
          //  todayHighlight: true
     });
	 
	 $('.clockpicker ').clockpicker({
        autoclose: true
    });
/** Events **/

// open popup in add Notification

$('#btn-new-ticket').click( function() {

	$("#member_id").select2("val", '');
	$("#nt_date1").val($.datepicker.formatDate("dd/mm/yy", new Date()));
    $("#st_note").val('');
    $("#add_btn_notification").html('Submit');
    $("#myModalLabel").html('Add Supportticket');
    // $('#new-ticket-wrapper').slideToggle("fast","linear")
    $("#myModal_client").modal('show');
});
// open popup in edit Notification

    $('.edit_notification').click( function() {
	//$('#new-ticket-wrapper').slideToggle("fast","linear")
		
		var st_id = $(this).attr('st_id');
        var st_date = $(this).attr('st_date');
		
		var st_time = $(this).attr('st_time');
		
        var st_note = $(this).attr('st_note');
        var member_id = $(this).attr('member_id');
        
		$("#st_id").val(st_id);
        $("#nt_date1").val(st_date);
		$("#st_time").val(st_time);
        $("#st_note").val(st_note);
		$("#member_id").select2("val", member_id);
		if (member_id == '') {
            $("#error_add_notification").show();
            $("#error_add_notification").html('Please select client');
            $("#member_id").css('border-color', 'red');
            return false;
        }
        $("#add_btn_notification").html('Update');
        $("#myModalLabel").html('Edit Supportticket');
		$("#myModal_client").modal('show');
	});


	$("#close_btn_notification").click(function(){
		$("#member_id").val('');
		$("#nt_date1").val('');
		$("#st_note").val('');
		$("#st_time").val('');
		
	});
	
	$("#add_btn_notification").click(function() {
	
		var st_id = $("#st_id").val();
		
        var member_id = $("#member_id").val();
		var nt_date1 = $("#nt_date1").val();
		var st_time = $("#st_time").val();
		var st_note = $("#st_note").val();

        $("#error_add_notification").hide();
         if (member_id == '') {
            $("#error_add_notification").show();
            $("#error_add_notification").html('Please select client');
            $("#member_id").css('border-color', 'red');
            return false;
        } else {
            var postdata = {};
            var obj = $(this);
            postdata['st_id'] = st_id;
			postdata['member_id'] = member_id;
			postdata['nt_date1'] = nt_date1;
			postdata['st_time'] = st_time;
			postdata['st_note'] = st_note;
         
            jQuery.post(BASE_PATH + '/supportticket/list', postdata, function(result) {
                if (result == 1) {
                    $("#success_add_notification").show();
                    window.setTimeout(function() {
                         $("#member_id").select2("val","");
                         $("#success_add_notification").hide();
                         $("#myModal_client").modal('hide');
                         window.location = BASE_PATH + 'supportticket/list';
                    }, 3000);
                }
            });
        }


    });

	// open popup in add Appointment
	
	$('#btn-new-appointment').click( function() {
		//$('#new-ticket-wrapper').slideToggle("fast","linear")
		$("#myModal_Appointment").modal('show');
	});
	
	$("#add_btn_appointment").click(function() {
	
	var app_member_id = $("#app_member_id").val();
	var property_type = $("#property_type").val();
	var property_subtype = $("#property_subtype").val();
	var prp_id = $("#property_id").val();
	var app_date = $("#app_date").val();
	var app_time = $("#app_time").val();
	var app_note = $("#app_note").val();

	$("#error_add_appointment").hide();
	if (app_member_id == '') {
		$("#error_add_appointment").show();
		$("#error_add_appointment").html('Please select Client');
		$("#app_member_id").css('border-color', 'red');
		return false;
	} else if (property_type == '') {
		$("#error_add_appointment").show();
		$("#error_add_appointment").html('Please select Property Type');
		$("#property_type").css('border-color', 'red');
		return false;
	} else if (property_subtype == '') {
		$("#error_add_appointment").show();
		$("#error_add_appointment").html('Please select Property SubType');
		$("#property_subtype").css('border-color', 'red');
		return false;
	} else if (prp_id == '') {
		$("#error_add_appointment").show();
		$("#error_add_appointment").html('Please select Property');
		$("#property_id").css('border-color', 'red');
		return false;
	} else {
		var postdata = {};
		var obj = $(this);
		postdata['app_member_id'] = app_member_id;
		postdata['property_type'] = property_type;
		postdata['property_subtype'] = property_subtype;
		postdata['prp_id'] = prp_id;
		postdata['app_date'] = app_date;
		postdata['app_time'] = app_time;
		postdata['app_note'] = app_note;
		jQuery.post(BASE_PATH + '/appointment/getappointmentform', postdata, function(result) {
			if (result == 1) {
				//alert(success_add_appointment);
				$("#success_add_appointment").show();
				//alert('imnnnn');
				window.setTimeout(function() {
					 //$("#member_id").select2("val","");
					 //$(".chek_all").prop("checked", false);
					 $("#success_add_appointment").hide();
					 $("#myModal_Appointment").modal('hide');
					  window.location = BASE_PATH + 'appointment/list';
				}, 3000);
			}
		});
	}
	});
	
	$('.property-select').change(function() {
		
        var postdata = {};
        var property_type = $(this).val();
       
            postdata['property_type'] = property_type;
            jQuery.post(BASE_PATH + 'property/getpropertysublist', postdata, function(result) {
                $('.property-subtype-select').html(result);
                
            });

    });
	
	$('.property-subtype-select').change(function() {
		
        var postdata = {};
		var property_subtype = $(this).val();
        var property_type = $('.property-select').val();
				
            postdata['property_type'] = property_type;
			
			postdata['property_subtype'] = property_subtype;
			
            jQuery.post(BASE_PATH + 'appointment/getpropertylist', postdata, function(result) {
                $('.property-select-property').html(result);
                
            });

    });

$('#btn-close-ticket').click( function() {
	//$('#new-ticket-wrapper').slideToggle("fast","linear")
});
	
$('#new-ticket-form').validate({
                focusInvalid: false, 
                ignore: "",
                rules: {
                    txtSubject: {
                        minlength: 2,
                        required: true
                    },
                    txtDept: {
						minlength: 2,
                        required: true,
                    },
                    txtMessage: {
                        required: true
                    }
                },

                invalidHandler: function (event, validator) {
					//display error alert on form submit    
                },

                errorPlacement: function (label, element) { // render error placement for each input type   
					$('<span class="error"></span>').insertAfter(element).append(label)
                    var parent = $(element).parent();
                    parent.removeClass('success-control').addClass('error-control');  
                },

                highlight: function (element) { // hightlight error inputs
                    var parent = $(element).parent();
                    parent.removeClass('success-control').addClass('error-control'); 
                },

                unhighlight: function (element) { // revert the change done by hightlight
                    
                },

                success: function (label, element) {
					var parent = $(element).parent();
					parent.removeClass('error-control').addClass('success-control'); 
                },

			    submitHandler: function(form) {
					$('#new-ticket-wrapper').slideToggle("fast","linear");
				}
            });	

	$('.grid .actions a.remove').on('click', function () {
            var removable = jQuery(this).parents(".grid");
            if (removable.next().hasClass('grid') || removable.prev().hasClass('grid')) {
                jQuery(this).parents(".grid").remove();
            } else {
                jQuery(this).parents(".grid").parent().remove();
            }
    });

     $('.grid .clickable').on('click', function () {
          var el = jQuery(this).parents(".grid").children(".grid-body");
		 // el.slideToggle(200);
    });


		
	$(".fa").click(function(){
         $('#nt_date').datepicker({
		 format: "dd/mm/yyyy",
                // startDate: '+1d'
		 });
    });
    $('#nt_date').datepicker({
            format: "dd/mm/yyyy",
           // startDate: '+1d',
            autoclose: true,
          //  todayHighlight: true
     });
	 
	$(".fa").click(function(){
         $('#app_date').datepicker({
		 format: "dd/mm/yyyy",
                // startDate: '+1d'
		 });
    });
    $('#app_date').datepicker({
            format: "dd/mm/yyyy",
           // startDate: '+1d',
            autoclose: true,
          //  todayHighlight: true
     });
     
    //$('.timepicker-default').timepicker();
   /* $('.timepicker-24').timepicker({
                minuteStep: 1,
                showSeconds: false,
                showMeridian: false,
                defaultTime: '00:00'
     });*/
	  
    $('.clockpicker').clockpicker({
        autoclose: true
    });
	$('.clockpicker').click(function(){
		$(".clockpicker-popover").css("z-index","10000");
	});
	//$('.clockpicker-popover').css('z-index','10000');
	

});

